package sctu.edu.java.year2016.team02.wanggaowen.day20160604.Chapter12_2;

import java.io.*;

/**
 * Created by 15c on 2016/6/5.
 *///12.2编写程序，使用字符输入、输出流读取文件，将一段文字加密后存入文件，然后再读取，
// 并将加密前与加密后的文件输出。
public class Secret {
    public static void main(String[] args){
        File file=new File("E://Secret.txt");

        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        try {
            FileWriter fileWriter=new FileWriter(file);


            fileWriter.write("java 让人尴尬");
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("输入完成：");


            try {

                FileReader fileReader=new FileReader(file);

                char[] st=new char[1024];

                int a=fileReader.read(st);

                String old=new String(st);

                System.out.println("未加密前：" + old);
                    byte l=5;
                byte[] by=old.getBytes();

                try {
                    for(int i=0;i<by.length;i++){
                        by[i]^=l;
                    }
                  String name= new String(by,0,by.length);

                    FileWriter fileWriter1=new FileWriter(file);

                    fileWriter1.write(name);

                    fileWriter1.close();

                    System.out.print(name);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

}

